package com.example.hssmgs;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.hssmgs.Student.CommonDatabase;
import com.example.hssmgs.Student.Student_login;
import com.example.hssmgs.Teacher.Teacher_login;
import com.example.hssmgs.admin.Admin_login;

public class MainActivity extends AppCompatActivity {

    private String status = ""; // 判断身份
    private SQLiteDatabase db;
//    private SharedPreferences pref;
//    private SharedPreferences.Editor editor;
//    private CheckBox rememberPass;
//    private boolean isRemember;
    private EditText edit_load_account;
    private EditText edit_load_password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 记住密码的实现

        // 状态


        // TextputLayout功能

        // 账号密码编辑框
        edit_load_account=findViewById(R.id.input_account);
        edit_load_password=findViewById(R.id.input_password);

        // 获取数据库对象
        CommonDatabase commonDatabase = new CommonDatabase();
        db = commonDatabase.getSqliteObject(MainActivity.this, "test.db");

        // 绑定组件
        final Button button_change_password = findViewById(R.id.button_change_password); // 修改密码按钮

        RadioGroup load_radiogroup = findViewById(R.id.load_radiogroup); // 单选组件

        final ImageButton imageButton = findViewById(R.id.imagebutton); // 显示密码按键

        // 眼睛显示密码和不显示密码

        // 单选按钮监听事件--身份
        load_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (group.getCheckedRadioButtonId())
                {
                    case R.id.radiobutton_student:
                        Toast toast=Toast.makeText(getApplicationContext(), "您选择了学生", Toast.LENGTH_SHORT);
                        toast.show();
                        button_change_password.setVisibility(View.VISIBLE);
                        status = "student";
                        break;

                    case R.id.radiobutton_teacher:
                        Toast toast2=Toast.makeText(getApplicationContext(), "您选择了老师", Toast.LENGTH_SHORT);
                        toast2.show();
                        button_change_password.setVisibility(View.VISIBLE);
                        status = "teacher";
                        break;

                    case R.id.radiobutton_admin:
                        Toast toast3=Toast.makeText(getApplicationContext(), "您选择了管理员", Toast.LENGTH_SHORT);
                        toast3.show();
                        button_change_password.setVisibility(View.VISIBLE);
                        status = "admin";
                        break;

                    default:
                        break;
                }
            }
        });

        // 登录按钮
        Button button_load = findViewById(R.id.button_load);
        button_load.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View v) {
                // 获取学生输入的账号
                String account_load = edit_load_account.getText().toString();

                String password_load = edit_load_password.getText().toString();

                if (status.equals("student"))
                {
                    String true_password = "";
                    // 获取每行的返回信息
                    Cursor cursor = db.query("student_pw",null,"account = ?",new String[]{account_load},null,null,null);

                    while (cursor.moveToNext())
                    {
                        true_password = cursor.getString(cursor.getColumnIndex("password"));
                    }

                    if (password_load.equals(true_password)){
                        Toast toast=Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT);
                        toast.show();
                        // 跳转页面
                        Intent intent = new Intent();

                        intent.setClass(MainActivity.this, Student_login.class);
                        // 把学号转过去
                        intent.putExtra("student_id",account_load);
                        startActivity(intent);
                    }else{
                        Toast toast=Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
                else if (status.equals("teacher"))
                {
                    String true_password = "";
                    // 获取每行的返回信息
                    Cursor cursor = db.query("teacher_pw",null,"account = ?",new String[]{account_load},null,null,null);

                    while (cursor.moveToNext())
                    {
                        true_password = cursor.getString(cursor.getColumnIndex("password"));
                    }

                    if (password_load.equals(true_password)){
                        Toast toast=Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT);
                        toast.show();
                        // 跳转页面
                        Intent intent = new Intent();

                        intent.setClass(MainActivity.this, Teacher_login.class);
                        // 把学号转过去
                        intent.putExtra("teacher_id",account_load);
                        startActivity(intent);
                    }else{
                        Toast toast=Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
                else if (status.equals("admin"))
                {
                    String true_password = "";
                    // 获取每行的返回信息
                    Cursor cursor = db.query("admin_pw",null,"account = ?",new String[]{account_load},null,null,null);

                    while (cursor.moveToNext())
                    {
                        true_password = cursor.getString(cursor.getColumnIndex("password"));
                    }

                    if (password_load.equals(true_password)){
                        Toast toast=Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT);
                        toast.show();
                        // 跳转页面
                        Intent intent = new Intent();

                        intent.setClass(MainActivity.this, Admin_login.class);
                        // 把管理员号转过去
                        intent.putExtra("admin_id",account_load);
                        startActivity(intent);
                    }else {
                        Toast toast = Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
                else
                {
                    Toast toast=Toast.makeText(getApplicationContext(), "您还没有选择任何登录类型", Toast.LENGTH_SHORT);
                    toast.show();
                }



            }
        });
        // 修改密码界面，跳转修改页面
        button_change_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, Change_pw.class);
                startActivity(intent);
            }
        });
        // 如果手机有记录可以记录显示，实现方法

        //


    }

}
